const axios = require('axios');
import {
    isStandardBrowserEnv
} from './global'

const getPgv = (c: string) => {
    return (c || '') + Math.round(2147483647 * (Math.random() || 0.5)) * (+new Date() % 1E10)
}

export class Request {
    constructor(defaults?: any) {
        defaults = defaults || {};
        defaults.headers = defaults.headers || {};
        if (!isStandardBrowserEnv) {
            defaults.headers['user-agent'] = defaults.headers['user-agent'] || 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36'
            defaults.headers['connection'] = defaults.headers['connection'] || 'close'
        }
        defaults.timeout = 1000 * 60;
        defaults.httpAgent = false;
        defaults.httpsAgent = false;

        this.axios = axios.create(defaults);
        if (!isStandardBrowserEnv) {
            this.Cookie = defaults.Cookie || {}
            this.Cookie['pgv_pvi'] = getPgv('');
            this.Cookie['pgv_si'] = getPgv('s');
            this.axios.interceptors.request.use((config: any) => {
                config.headers['cookie'] = Object.keys(this.Cookie).map(key => {
                    return `${key}=${this.Cookie[key]}`
                }).join('; ');
                return config;
            }, (err: any) => {
                return Promise.reject(err);
            });

            this.axios.interceptors.response.use((res: any) => {
                let setCookie = res.headers['set-cookie']
                if (setCookie) {
                    setCookie.forEach((item: string) => {
                        let pm = item.match(/^(.+?)\s?\=\s?(.+?);/);
                        if (pm) {
                            this.Cookie[pm[1]] = pm[2];
                        }
                    })
                }
                return res;
            }, (err: any) => {
                if (err && err.response) {
                    delete err.response.request
                    delete err.response.config
                }
                return Promise.reject(err)
            });
        }

        // this.request = options => {
        //     return this.axios.request(options);
        // }
    }
    public async request(options: any) {
        return this.axios.request(options);
        // try {
        //     let r = await this.axios.request(options);
        //     return r;
        // } catch (err) {
        //     throw err;
        // }

    }
    private axios: any;
    private Cookie: any;
    //  public request;
}